﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace PublishNuget
{
    class Program
    {
        static void Main(string[] args)
        {
            var appFolder = System.Environment.CurrentDirectory.Replace("\\PublishNuget","");
            var files = System.IO.Directory.GetFiles(appFolder, "Colorful.*.nupkg", SearchOption.AllDirectories);
            files = files.Where(a => a.Contains("\\Release\\")).ToArray();
            if (args.Length>0 && !string.IsNullOrEmpty(args[0]))
            {
                files = files.Where(a => a.Contains(args[0])).ToArray();
            }
            files = files.OrderByDescending(a => a).ToArray();
            var publishList = new List<string>();
            foreach (var file in files)
            {
                var fileName = Path.GetFileName(file);
                fileName = fileName.Substring(0, fileName.IndexOf(Regex.Match(fileName, @"(\.[0-9.]{4,})\.").Groups[1].Value));
                if (publishList.Contains(fileName)) continue;
                publishList.Add(fileName);
                Console.WriteLine("正在发布 {0} ...", Path.GetFileName(file));
                var process = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("nuget")
                {
                    CreateNoWindow = true,
                    Arguments = $"push {file} 7d4c9f62-57ce-410f-bc5f-a82e05ebb68f -Source https://www.nuget.org/api/v2/package",
                    WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
                });
                process.WaitForExit();
                if (process.ExitCode == 0)
                    Console.WriteLine("发布成功！");
                else
                    Console.WriteLine("发布失败！");
            }
            Console.WriteLine("发布完成！按任意键退出...");
            Console.ReadKey(true);
        }
    }
}
